package cn.iocoder.yudao.framework.prometheus.ws;

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;

import javax.annotation.Resource;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

public class WebSocketMessageMetrics {

    private final ConcurrentMap<String, Counter> messageCounters = new ConcurrentHashMap<>();

    @Resource
    private MeterRegistry registry;

    public WebSocketMessageMetrics() {
    }

    // 增加连接数
    public void recordMetrics(String path) {
        Counter counter = messageCounters.computeIfAbsent(path, k ->
                Counter.builder("websocket.messages.total")
                        .description("WebSocket 消息总数")
                        .tag("protocol", "ws")  // 可选：添加标签区分协议类型
                        .tag("provider", "netty")
                        .tag("uri", path)
                        .register(registry)
        );
        counter.increment();
    }

}
